Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply double check pattern to avoid race condition in LoggingService #11284

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

YuliiaKovalova
Copy link
Member

@YuliiaKovalova YuliiaKovalova commented Jan 14, 2025

Fixes: https://dev.azure.com/devdiv/DevDiv/_workitems/edit/2320135/

Context

Reset() method invocation sometimes causes exception System.IO.IOException: 'The handle is invalid.'.
Very likely it happens on application shutdown.
The assumption is:

  • ShutdownComponent starts and gets the _lockObject
  • It cleans up/disposes the events
  • Meanwhile our logging thread is trying to use those same events without checking _lockObject in context of StartLoggingEventProcessing()

Changes Made

To resolve this issue, the double-check pattern was implemented for the event fields.
This double-check approach prevents the 'invalid handle' exception from occurring during the race condition between component shutdown and the execution of the logging thread.

The changes include:

  1. Introduction of _dequeueEventDoubleCheckCopy, _emptyQueueEventDoubleCheckCopy, and _enqueueEventDoubleCheckCopy fields to store local copies of the corresponding events.
  2. Modification of the StartLoggingEventProcessing method:
    • The copy fields are initialized with the original event fields immediately after creation.
    • The LoggingEventProc local function now uses the copy fields instead of the original event fields.
  3. Update of the CleanLoggingEventProcessing method:
    • The copy fields are set to null after disposing of the original events.

By using local copies of the events within the logging thread, we ensure that the logging thread operates independently of the state of the original event fields.
The original event fields still serve as the single source of truth and are used to initialize the copies.
However, the copies are used exclusively by the logging thread, isolating it from any disposal that occurs on the main thread.

Testing

N/A

@YuliiaKovalova YuliiaKovalova marked this pull request as ready for review January 15, 2025 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant